# !python3      # 使用工具:PyCharm
# 用户登录名:yyds_l
# 项目名称: python爬虫     文件名称:使用CSS语法查找元素1
# 系统日期:2022/3/16      系统时间:9:03
# 2022/03/16 09:03

"""ok"""
from bs4 import BeautifulSoup
html = """

<html><head><title>The Dormouse's story</title></head>

<body>

<p class="title" name="dromouse"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were

<a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>,

<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and

<a href="http://example.com/tillie/1" class="sister" id="link3">Tillie</a>;

and they lived at the bottom of a well.</p>

<p class="story" id="p1"><i>ios</i>...qa</p>

"""


def print_hi():
    # 异常捕捉
    try:
        soup = BeautifulSoup(html, "html.parser")
        # print(soup.prettify())
        # # 通过标签查找元素
        # print(soup.select("title")[0])
        # print(soup.select("p")[0])
        # # 通过类名查找
        # print(soup.select('.sister')[:2])
        # # 通过id名查找
        # print(soup.select('#p1')[0])
        # # 组合查找
        # print(soup.select('p #link2'))
        # print(soup.a['href'])
        # # 属性查找
        # print(soup.select('p > b')[0].text)
        """"""
        # print(soup.select('p a[href*="http://example.com"]'))
        index = 0
        for i in soup.select('p a[href*="http://example.com"]'):
            index = (index + 1)*2
            # print(index)
            print(i['href']+"/id="+str(index))

        # 多线程调用
        print_hi_two()
        print("i")
    except Exception as err:
        print(err)


def print_hi_two():
    # 异常捕捉
    try:
        print("i2")
    except Exception as err:
        print(err)


if __name__ == '__main__':
    print_hi()
